package net.xumf.web.controller.quartz.normal;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xumf.service.master.QuartzService;
import net.xumf.support.consts.Constants;
import net.xumf.support.model.ResponseResult;
import net.xumf.support.model.dto.QuartzDTO;
import net.xumf.support.model.vo.CronTriggerVO;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(value = "普通定时任务管理", description = "普通定时任务管理")
@RestController
@RequestMapping(value = "quartz/normal")
public class QuartzNormalController {

    @Autowired
    private QuartzService quartzService;

    @ApiOperation(value = "获取所有普通定时任务数据")
    @GetMapping(value = Constants.UrlPath.GET_ALL)
    public List<CronTriggerVO> getAllNormalJob() throws SchedulerException {
        return quartzService.getAllNormalJob();
    }

    @ApiOperation(value = "新增普通定时任务")
    @PostMapping(value = "add")
    public ResponseResult addJob(@ApiParam @Valid QuartzDTO dto, BindingResult result) throws SchedulerException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            return ResponseResult.failure(fieldError.getDefaultMessage());
        }
        quartzService.addNormalJob(dto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "暂停普通定时任务")
    @PostMapping(value = "/pause")
    public ResponseResult pauseJob(@ApiParam(name = "任务名", value = "jobName", required = true) @RequestParam(value = "jobName") String jobName) throws SchedulerException {
        quartzService.jobPause(jobName, Constants.Normal.NORMAL_GROUP_NAME);
        return ResponseResult.success();
    }

    @ApiOperation(value = "重启普通定时任务")
    @PostMapping(value = "/resume")
    public ResponseResult resumeJob(@ApiParam(name = "任务名", value = "jobName", required = true) @RequestParam(value = "jobName") String jobName) throws SchedulerException {
        quartzService.jobResume(jobName, Constants.Normal.NORMAL_GROUP_NAME);
        return ResponseResult.success();
    }

    @ApiOperation(value = "更新普通定时任务")
    @PostMapping(value = "/reschedule")
    public ResponseResult rescheduleJob(@ApiParam @Valid QuartzDTO dto, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            return ResponseResult.failure(fieldError.getDefaultMessage());
        }
        quartzService.jobNormalReschedule(dto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "删除普通定时任务")
    @PostMapping(value = Constants.UrlPath.ID_PATH_ACTION)
    public ResponseResult deleteJob(@ApiParam(name = "任务名", value = "jobName", required = true) @RequestParam(value = "jobName") String jobName) throws SchedulerException {
        quartzService.jobDelete(jobName, Constants.Normal.NORMAL_GROUP_NAME);
        return ResponseResult.success();
    }

}
